package com.sf.fy.uav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.common.enums.CommJumpTypeEnum;
import com.sf.fy.uav.common.enums.SettingStatusEnum;
import com.sf.fy.uav.common.request.common.IdRequest;
import com.sf.fy.uav.common.request.operate.SysBannerRequest;
import com.sf.fy.uav.common.request.order.DealRequest;
import com.sf.fy.uav.common.response.operational.BannerResponse;
import com.sf.fy.uav.common.response.operational.SysBannerResponse;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.entity.Banner;
import com.sf.fy.uav.entity.File;
import com.sf.fy.uav.mapper.BannerMapper;
import com.sf.fy.uav.mapper.FileMapper;
import com.sf.fy.uav.service.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2023-06-28
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {
    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private FileMapper fileMapper;

    @Override
    public Long getTotal(String name, Integer status) {
        QueryWrapper<Banner> bannerQueryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(name)) {
            bannerQueryWrapper.like("name", name);
        }
        if (!ObjectUtils.isEmpty(status)) {
            bannerQueryWrapper.eq("status", status);
        }
        bannerQueryWrapper.eq("deleted", false);
        return bannerMapper.selectCount(bannerQueryWrapper);
    }

    @Override
    public List<SysBannerResponse> getBanners(SysBannerRequest sysBannerRequest) {
        QueryWrapper<Banner> bannerQueryWrapper = new QueryWrapper<>();
        Page<Banner> bannerPage = new Page<>(sysBannerRequest.getPage(), sysBannerRequest.getSize());
        String name = sysBannerRequest.getName();
        Integer status = sysBannerRequest.getStatus();
        if (!ObjectUtils.isEmpty(name)) {
            bannerQueryWrapper.like("name", name);
        }
        if (!ObjectUtils.isEmpty(status)) {
            bannerQueryWrapper.eq("status", status);
        }
        bannerQueryWrapper.eq("deleted", false);
        bannerQueryWrapper.orderByDesc("update_time");
        IPage<Banner> bannerIPage = bannerMapper.selectPage(bannerPage, bannerQueryWrapper);
        List<Banner> banners = bannerIPage.getRecords();
        List<String> fileIds = banners.stream().map(banner -> banner.getFileId()).collect(Collectors.toList());
        List<SysBannerResponse> sysBannerResponses = new ArrayList<>();
        if (!CollectionUtils.isEmpty(fileIds)) {
            QueryWrapper<File> fileQueryWrapper = new QueryWrapper<>();
            fileQueryWrapper.in("id", fileIds);
            List<File> files = fileMapper.selectList(fileQueryWrapper);
            banners.forEach(banner -> {
                SysBannerResponse sysBannerResponse = BeanUtil.copyProperties(banner, SysBannerResponse.class);
                sysBannerResponse.setJumpTypeName(CommJumpTypeEnum.getDesc(sysBannerResponse.getJumpType()));
                files.forEach(file -> {
                    if (file.getId().equals(banner.getFileId())) {
                        sysBannerResponse.setCover(file.getFilePath());
                    }
                });
                sysBannerResponses.add(sysBannerResponse);
            });

        }
        return sysBannerResponses;
    }

    @Override
    public List<BannerResponse> getBanners() {
        Date date = new Date();
        QueryWrapper<Banner> bannerQueryWrapper = new QueryWrapper<>();
        bannerQueryWrapper.ne("status", SettingStatusEnum.OFF.getStatus());
        bannerQueryWrapper.eq("deleted", false);
        bannerQueryWrapper.orderByAsc("sort");
        bannerQueryWrapper.le("start_time", date);
        bannerQueryWrapper.ge("end_time", date);
        List<Banner> banners = bannerMapper.selectList(bannerQueryWrapper);
        List<String> fileIds = banners.stream().map(Banner::getFileId).collect(Collectors.toList());
        List<BannerResponse> bannerResponses = new ArrayList<>();
        if (!CollectionUtils.isEmpty(fileIds)) {
            QueryWrapper<File> fileQueryWrapper = new QueryWrapper<>();
            fileQueryWrapper.in("id", fileIds);
            List<File> files = fileMapper.selectList(fileQueryWrapper);
            banners.forEach(banner -> {
                BannerResponse bannerResponse = BeanUtil.copyProperties(banner, BannerResponse.class);
                files.forEach(file -> {
                    if (file.getId().equals(banner.getFileId())) {
                        bannerResponse.setCover(file.getFilePath());
                    }
                });
                bannerResponses.add(bannerResponse);
            });

        }
        return bannerResponses;
    }

    @Override
    public void off(DealRequest dealRequest) {
        Banner banner = bannerMapper.selectById(dealRequest.getId());
        if (ObjectUtils.isEmpty(banner)) {
            throw new RuntimeException("banner不存在");
        }
        banner.setProcessedBy(dealRequest.getProcessedBy());
        banner.setStatus(SettingStatusEnum.OFF.getStatus());
        banner.setUpdateTime(new Date());
        bannerMapper.updateById(banner);
    }

    @Override
    public Integer start(IdRequest idRequest) {
        Banner banner = bannerMapper.selectById(idRequest.getId());
        if (!ObjectUtils.isEmpty(banner)) {
            Date startTime = banner.getStartTime();
            Date endTime = banner.getEndTime();
            Date date = new Date();
            if (startTime.getTime() > date.getTime()) {
                //还没开始
                banner.setStatus(SettingStatusEnum.NOT_STARTED.getStatus());
            } else {
                if (date.getTime() < endTime.getTime()) {
                    banner.setStatus(SettingStatusEnum.ON_DISPLAY.getStatus());
                } else {
                    banner.setStatus(SettingStatusEnum.ENDED.getStatus());
                }
            }
            bannerMapper.updateById(banner);
            return banner.getStatus();
        }
        return null;
    }

    @Override
    public Integer end(IdRequest idRequest) {
        Banner banner = bannerMapper.selectById(idRequest.getId());
        if (!ObjectUtils.isEmpty(banner)) {
            Date startTime = banner.getStartTime();
            Date endTime = banner.getEndTime();
            Date date = new Date();
            if (startTime.getTime() > date.getTime()) {
                //还没开始
                banner.setStatus(SettingStatusEnum.NOT_STARTED.getStatus());
            } else {
                if (date.getTime() < endTime.getTime()) {
                    banner.setStatus(SettingStatusEnum.ON_DISPLAY.getStatus());
                } else {
                    banner.setStatus(SettingStatusEnum.ENDED.getStatus());
                }
            }
            bannerMapper.updateById(banner);
            return banner.getStatus();
        }
        return null;
    }


    @Override
    public void delete(DealRequest dealRequest) {
        Banner banner = bannerMapper.selectById(dealRequest.getId());
        if (ObjectUtils.isEmpty(banner)) {
            throw new RuntimeException("banner不存在");
        }
        banner.setProcessedBy(dealRequest.getProcessedBy());
        banner.setDeleted(true);
        banner.setUpdateTime(new Date());
        bannerMapper.updateById(banner);
    }

    @Override
    public SysBannerResponse getById(String bannerId) {
        Banner banner = bannerMapper.selectById(bannerId);
        if (ObjectUtils.isEmpty(banner)) {
            throw new RuntimeException("记录不存在");
        }
        return BeanUtil.copyProperties(banner, SysBannerResponse.class);
    }
}
